package aad.storyboard.model;

import java.io.FileNotFoundException;
import java.util.ArrayList;

import aad.storyboard.model.GraphicalObject;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;


public abstract class Image extends GraphicalObject implements Cloneable{

        public static final int TYPE_BITMAP = 1;
        public static final int TYPE_VECTOR = 2;
        
        private String name;
        private ArrayList<Animation> animations;
        private Animation onTouchAnimation;     
        
        protected Bitmap bitmap;
        protected Bitmap bitmapBuffer;
        protected boolean bitmapChanged;
        protected float height;
        protected float width;
        
        private Image fadeTarget;	
		private Bitmap blurBitmap;
		private Paint blurPaint;
		private String info;
        
        public Image(Resources res, int ID) throws FileNotFoundException{
        	
                this.bitmap = this.constructBitmap(res,ID);
                this.constructDrawable(res,ID);
                Log.e("BITMAP",bitmap.getWidth()+","+bitmap.getHeight());
                this.bitmapBuffer = this.bitmap;
                paint = new Paint();
                paint.setColor(Color.LTGRAY);
                paint.setStyle(Paint.Style.FILL);
                paint.setAlpha(0);
                bitmapChanged = false;
                this.name = res.getString(ID);
        }
        

		protected Image() {
			// TODO Auto-generated constructor stub
		}


		protected abstract Bitmap constructBitmap(Resources res, int iD) throws FileNotFoundException;
        
        protected abstract Drawable constructDrawable(Resources res, int iD);
        
        public Bitmap getBitmap(){
                if(bitmapChanged){
                        this.bitmap = Bitmap.createScaledBitmap(this.bitmapBuffer, this.getWidth(), this.getHeight(), false);
                        bitmapChanged = false;
                }
                return this.bitmap;
        }
        
        public int getHeight(){
                return Math.round(this.height);
        }

        public void setHeight(float height){
                this.bitmapChanged = true;
                this.height = height;
        }

        public int getWidth(){
                return Math.round(this.width);
        }
        
        public float getPreciseWidth(){
        	return this.width;
        }
        public float getPreciseHeight(){
        	return this.height;
        }

        public void setWidth(float width){
                this.bitmapChanged = true;
                this.width = width;
        }
        
        public void setName(String n){
                name = n;
        }
          public String getInfo(){
                return info;
        }
        
        public void setInfo(String n){
            info = n;
    }
        public String getName(){
                return name;
        }

        public abstract Drawable getDrawable();
        

        public Object getTransitions() {
                
                return null;
        }

        public Image getFadeTarget(int currentTime) {           
                return fadeTarget;
        }

        public void setFadeTarget(Image fadeTarget) {
                this.fadeTarget = fadeTarget;
        }

        public void setBitmapBuffer(Bitmap nBitmap) {
                bitmapChanged = true;
                this.bitmapBuffer = nBitmap;
                
        }

        public Bitmap getBitmapBuffer() {
                return this.bitmapBuffer;
        }
        
        public Image clone() throws CloneNotSupportedException{
					return (Image) super.clone();
        }

		public void scaleToWidth(int w) {
			float currentHeight = this.height;
			float currentWidth = this.width;
			float newHeight = w*currentHeight/currentWidth;
			this.width = w;
			this.height = newHeight;
			this.bitmapChanged = true;
			
		}
		public void scaleToHeight(int h) {
			float currentHeight = this.height;
			float currentWidth = this.width;
			float newHeight = h*currentWidth/currentHeight;
			this.width = h;
			this.height = newHeight;
			this.bitmapChanged = true;
			
		}
		
		

		public int getPosX() {
			//Log.e("tag", "posx: "+(this.posX-(this.bitmap.getWidth()/2))+" because width:"+this.bitmap.getWidth());
			int startPosX = (int)Math.ceil(this.posX-(this.bitmap.getWidth()/2));
			return startPosX;
		}
		
		public int getPosY() {
			//Log.e("tag", "posy: "+(this.posX-(this.bitmap.getWidth()/2)));
			int startPosY = (int)Math.ceil(this.posY-(this.bitmap.getHeight()/2));
			return startPosY;
		}
public void setBlurBitmap(Bitmap alphaBitmap) {
			this.blurBitmap=alphaBitmap;
			
		}
		
		public Bitmap getBlurBitmap()
		{
			return blurBitmap;
		}


		public void setblurPaint(Paint blurPaint) {
			this.blurPaint=blurPaint;
			
		}


		public Paint getBlurPaint() {
			// TODO Auto-generated method stub
			return blurPaint;
		}


		public void clearBlurEffect() {
			this.blurPaint=null;
			this.blurBitmap=null;
			
		}


		public abstract Image createImage(Resources r, int id) throws FileNotFoundException;
		
}
